/**
 * Created with IntelliJ IDEA
 * Description:
 * User:恋恋
 * Date:2022-03-25
 * Time:19:59
 */
import java.util.Scanner;
public class Test {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,7,8,9};
        //建立左下标，右下标，中间下标
        int right = arr.length - 1;
        int len = 0;
        Scanner in=new Scanner(System.in);
        System.out.print("请输入想寻找的数字：");
        int key = in.nextInt();
        //限制条件
        while (right >=len) {
            int mid = (right + len) / 2;
            if (arr[mid] < key) {
                len = mid + 1;
            } else if (arr[mid] > key) {
                right = mid - 1;
            } else if(arr[mid]==key){
                System.out.println(key + "找到啦,在第" + (mid + 1) + "个");
                break;
            }
        }
        if(len>right) {
            System.out.println("找不到 " + key);
        }
    }
}
